package com.minsu.service.impl;

import com.minsu.entity.Fuel;
import com.minsu.entity.PageRequest;
import com.minsu.entity.PageResult;
import com.minsu.entity.ProductionOrder;
import com.minsu.mapper.FuelMapper;
import com.minsu.service.FuelService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Service
public class FuelServiceImpl implements FuelService {

    @Autowired
    private FuelMapper fuelMapper;


    //    @Override
//    public List<Fuel> findAllFuel(int pageNum, int pageSize) {
//        List<Fuel> fuels = fuelMapper.findAllFuel();
//        return fuels == null ? Collections.emptyList() : fuels;
//    }
//    @Override
//    public List<Fuel> findAllFuel(Integer pageNum, Integer pageSize) {
//        int offset = (pageNum - 1) * pageSize;
//        List<Fuel> fuels = fuelMapper.findAllFuel(offset, pageSize);
//        return fuels == null ? Collections.emptyList() : fuels;
//    }
    @Override
    public PageResult<Fuel> findAllFuel(PageRequest pageRequest) {
        // 查询总记录数
        Long totalCount = fuelMapper.countAllFuel();

        // 计算偏移量
        int offset = (pageRequest.getPageNum() - 1) * pageRequest.getPageSize();

        // 查询当前页数据
        List<Fuel> fuels = fuelMapper.findAllFuel(offset, pageRequest.getPageSize());

        // 返回分页结果
        return PageResult.<Fuel>builder()
                .pageNum(pageRequest.getPageNum())
                .pageSize(pageRequest.getPageSize())
                .totalCount(totalCount)
                .totalPages((int) Math.ceil((double) totalCount / pageRequest.getPageSize()))
                .data(fuels)
                .build();
    }

    ;


    @Override
    public void addFuel(Fuel fuel) {
        LocalDateTime now = LocalDateTime.now();
        fuel.setInTime(LocalDateTime.now());
        LocalDate today = now.toLocalDate();
        // 从数据库查询当天的最大批次号（假设批次号格式为yyyyMMddXXX）
        String maxBatchNo = fuelMapper.getMaxBatchNoByDate(today); // 自定义数据库查询方法

        int nextSuffix = 1; // 默认为当天第一个批次
        if (maxBatchNo != null && !maxBatchNo.isEmpty()) {
            // 提取后缀数字部分（假设批次号为yyyyMMdd+3位数字，如20250602001）
            String suffixStr = maxBatchNo.substring(8); // 截取后3位
            nextSuffix = Integer.parseInt(suffixStr) + 1;
        }

        // 生成新批次号（补前导零，确保3位）
        String batchNo = today.format(DateTimeFormatter.BASIC_ISO_DATE) +
                String.format("%03d", nextSuffix); // 如20250602001

        fuel.setBatchNo(batchNo);
        fuel.setOutTime(LocalDateTime.now());

        fuelMapper.addFuel(fuel);
    }

    @Override
    public void deleteById(String batchNo) {
        fuelMapper.deleteById(batchNo);
    }

    @Override
    public Fuel findByBatchNo(String batchNo) {
        return fuelMapper.findByBatchNo(batchNo);
    }

    @Override
    public void updateFuel(Fuel fuel) {

        fuel.setOutTime(LocalDateTime.now());
        fuelMapper.updateFuel(fuel);

    }

}
